<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                              |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liuxuliang@clcw.com.cn  Blog:http://blog.csdn.net/samxx8       |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\CarResource;

use AuctionBack\Model\DealerAuctionedModel;
use AuctionCore\AuctionOrder\CarReturn;
use AuctionCore\AuctionOrder\Recheck;
use AuctionCore\PurchaseOrder\PurchaseOrder;
use AuctionCore\System\AuctionException;
use AuctionCore\Account\Back\Employee;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\CarDealer\Dealer;
use AuctionCore\CarDealer\MyAuctioned;
use AuctionCore\Other\Inbox;
use AuctionCore\Issue\DealerIssue;
use Think\Log;
use AuctionCore\ChangeOrder\ChangeOrder;
use AuctionCore\AntNest\UserCenter;

/**
 * 车商争议模型
 * @author liuxuliang
 */
class CarCheck
{
    const STATUS_PENDING = 0; // 待验车
    const STATUS_FOLLOWING = 1; // 跟踪中
    const STATUS_SUCCESS = 2; // 验车成功
    const STATUS_FAILD = 3; // 验车失败
    const STATUS_VIOLATE = 4; // 违约
    const STATUS_LOCKED = 5; // 锁定

    const TRANSFER_BY_COMPANY = 0; // 公司过户
    const TRANSFER_BY_PERSON = 1; // 车商自己过户

    public static $dataStatus = array(
        'pending' => self::STATUS_PENDING,
        'following' => self::STATUS_FOLLOWING,
        'success' => self::STATUS_SUCCESS,
        'faild' => self::STATUS_FAILD,
        'violate' => self::STATUS_VIOLATE,
        'locked' => self::STATUS_LOCKED,
    );

    public static $transferType = array(
        'company' => self::TRANSFER_BY_COMPANY,
        'person' => self::TRANSFER_BY_PERSON,
    );

    protected $order_id = null;

    /**
     * 构造
     * @param    int $order_id 拍单编号 或 拍单id
     * @return   void
     */
    public function __construct($order_no_or_id = '')
    {
        $this->order_id = $order_no_or_id;
    }

    private function checkOrderID()
    {
        if (!is_numeric($this->order_id)) {
            $r = M('order')->field('order_id')->where("order_no='{$this->order_id}'")->find();
            if ($r && count($r) > 0) {
                $this->order_id = $r['order_id'];
            } else {
                AuctionException::make_throw('700002');
            }
        }
    }

    /**
     * 拍单详情，以及拍单状态等。
     * @return   Array
     */
    public static function detail_by_check_id($check_id)
    {
        $cond = array(
            'check_id' => $check_id
        );
        $res = M('car_dealer_check')->where($cond)->find();
        return $res;
    }

    public static function detail_by_order_id($order_id)
    {
        $cond = array(
            'order_id' => $order_id
        );
        $res = M("car_dealer_check")->where($cond)->find();
        return $res;
    }

    /**
     * 获取车商验车数据详情
     *
     * 拍单、车源、车主信息、评估师信息、复检信息
     *
     * @param int $issueStatus 车商争议数据状态(0:待处理, 1:跟踪中 2:协商成功 , 3:协商失败)
     *
     * @return mixed
     **/
    public function car_dealer_check_detail()
    {
        //要查询的详情
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.bid_best_price,o.bidding_best_price,o.bidding_end_time,o.bidding_start_time,o.act_elapsed_time,o.first_pay_status,o.is_consult,o.company_subsidies,
            o.success_price,o.owner_confirm_service_id,o.fail_type,o.first_money,o.tail_money,o.is_self_confirm,o.last_time,o.reserve_price,o.transfer_type,
            c.sid,c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,c.brand_id,c.series_id,c.model_id,co.seller_name,co.phone,co.sign_reserve_time,c.emission,c.first_reg_date,c.pointer_mileage,c.reg_city,
            c.vin,c.car_color,c.accident_level,c.transfer_limit,c.composite_state,c.history_state,c.car_source,
            co.sign_reserve_province,co.sign_reserve_store,co.sign_service_method,co.sign_date,co.check_date,co.owner_id,
            co.sign_reserve_city,co.sign_reserve_area,co.sign_reserve_address,co.deliver_id,o.is_sign_tracing,co.comefrom,co.comefrom_url,
            cd.dealer_id,cd.contact_person,cd.level,du.uid,
            em.real_name as checker_name,em.mobile as checker_mobile,
            dc.check_id,dc.man_id,dc.status as check_status,dc.check_time as check_done_time,dc.check_limit_time,dc.updatetime as check_update_time,dc.emp_id as check_done_empid,
            dc.remark as dealer_check_remark,dc.updatetime,
            emdc.real_name as check_done_emp";
        $prefix = C('DB_PREFIX');
        $cond = [
            'o.order_id' => $this->order_id
        ];
        $res = M('order')->alias('o')->field($cols)
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join('LEFT JOIN ' . $prefix . "car_dealer as cd ON cd.dealer_id=o.success_dealer_id")
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->join('LEFT JOIN ' . $prefix . "employee as em ON em.emp_id=co.checker_id")
            ->join('LEFT JOIN ' . $prefix . "car_dealer_check as dc ON dc.order_id=o.order_id")
            ->join('LEFT JOIN ' . $prefix . "employee as emdc ON emdc.emp_id=dc.emp_id")
            ->where($cond)->find();
        $dealer = new Dealer($res['dealer_id']);
        $dealerInfo = $dealer->baseInfo();
        $res['contact_mobile'] = $dealerInfo['contact_mobile'];
        //M('order')->getLastSql();
        $cm = new \AuctionCore\CarResource\CarModel();
        $brand = $cm->get_model_by_id($res['brand_id'], 'brand');
        $series = $cm->get_model_by_id($res['series_id'], 'series');
        $model = $cm->get_model_by_id($res['model_id']);
        $res['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $res['format_reserve_price'] = format_money($res['reserve_price']);
        $res['format_best_price'] = format_money($res['bidding_best_price'] > $res['bid_best_price'] ? $res['bidding_best_price'] : $res['bid_best_price']);
        $res['best_price'] = $res['bidding_best_price'] > $res['bid_best_price'] ? $res['bidding_best_price'] : $res['bid_best_price'];
        $res['car_color'] = C('CAR_COLOR.' . $res['car_color']);
        $res['emission'] = C('INT_EMISSION_STD.' . $res['emission']);
        $res['plate_prefix'] = C('PLATE_PREFIX.' . $res['plate_prefix']);
        $res['pointer_mileage'] = formatMileage($res['pointer_mileage']);
        $addr = new \AuctionCore\Other\Address();
        $res['location_area_name'] = $addr->get_name_by_code($res['location_area']);
        $res['reg_city'] = $addr->get_name_by_code($res['reg_city']);
        $res['car_plate_number'] = C('PLATE_PREFIX')[$res['plate_prefix']] . $res['plate_suffix'];
        //获取回车信息
        if ($res['car_source'] == 1) {
            $carReturnInfo = CarReturn::getCarReturnInfoBySid($res['sid']);
            $res['driver_name'] = $carReturnInfo['driver_name'];
            $res['driver_company'] = $carReturnInfo['driver_company'];
            $res['driver_phone'] = $carReturnInfo['driver_phone'];
            $res['depart_time'] = $carReturnInfo['depart_time'];
        }
        //crl.driver_name,crl.delive_store,crl.driver_company,crl.driver_phone,crl.depart_time,

        //获取复检信息
        $recheckInfo = Recheck::getRecheckInfoBySid($res['sid']);
        $res['condition_checker_name'] = $recheckInfo['condition_checker_name'];
        $res['procedure_checker_name'] = $recheckInfo['procedure_checker_name'];
        $res['procedures'] = $recheckInfo['procedures'];
        $res['next_trace_time'] = $recheckInfo['next_trace_time'];
        $res['recheck_time'] = $recheckInfo['recheck_time'];
        $res['recheck_remark'] = $recheckInfo['remark'];

        return $res;
    }

    /**
     * 获取车商验车列表
     * @param int $page
     * @param int $size
     * @param $params
     * @return array
     */
    public function dealer_check_car_list($page = 1, $size = 10, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;

        // $order = "o.last_time desc";
        if ($params['status'] < 2) {
            $order = "dc.check_limit_time asc";
        } else {
            $order = "dc.updatetime desc";
        }


        $map = array();
        //权限限制
        if ($params['status'] != self::STATUS_PENDING && $params['auth'] == false) {
            $map['dc.emp_id'] = $params['emp_id'];
        }
        //$map['o.status'] = ['neq',12];
        if (isset($params['status']) && is_numeric($params['status'])) {
            // $map .= "di.status=" . $params['status'];
            if ($params['status'] == 0 || $params['status'] == 1) {
                $map['dc.status'] = $params['status'];
                $map['o.status'] = 8;
            } elseif ($params['status'] == 3) { // 在验车失败列表中展示车商违约数据
                $map['dc.status'] = ['egt', 3];
            } elseif ($params['status'] == 5) { //已锁定，即拍单锁定
                $map['o.is_lock'] = ['eq', 1];
            } else {
                $map['dc.status'] = $params['status'];
            }
        }
        if (!empty($params['location_area'])) {
            $map['c.location_area'] = $params['location_area'];
        }
        if (!empty($params['comeFrom'])) {
            $map['co.comefrom'] = $params['comeFrom'];
        }

        if (!empty($params['order_no'])) {
            if (is_numeric($params['order_no'])) {
                $uc = new UserCenter();
                $info1 = $uc->info_bymobile($params['order_no'], 2, 1);
                $where['du.uid'] = array('eq', $info1['account_id']);
            } else {
                $where['o.order_no'] = array('eq', $params['order_no']);
                $where['cd.contact_person'] = array('eq', $params['order_no']);
                $where['_logic'] = 'or';
            }
            $map['_complex'] = $where;
        }

        //要查询的列表
        $cols = "o.order_id,o.order_no,o.status,o.car_id,o.last_time,o.is_lock,
            c.car_id,c.car_no,c.plate_suffix,c.plate_prefix,c.location_area,c.brand_id,c.series_id,c.model_id,co.seller_name,co.phone,co.sign_reserve_time,
            c.vin,c.car_color,c.accident_level,c.transfer_limit,c.composite_state,c.history_state,
            co.sign_reserve_province,co.sign_reserve_store,co.sign_service_method,co.sign_date,co.checker_name,
            co.sign_reserve_city,co.sign_reserve_area,co.sign_reserve_address,co.deliver_id,o.is_sign_tracing,co.comefrom,co.comefrom_url,
            cd.contact_person,cd.dealer_id,du.uid,
            dc.check_id,dc.emp_id,dc.man_id,dc.check_time,dc.check_limit_time";
        $prefix = C('DB_PREFIX');
        $total = M('order')->alias('o')
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id=o.success_dealer_id")
            ->join($prefix . "dealer_user as du ON cd.dealer_id=du.dealer_id")
            ->join($prefix . "car_dealer_check as dc ON dc.order_id=o.order_id")
            ->where($map)->count();

        $list = M('order')->alias('o')->field($cols)
            ->join($prefix . "cars as c ON c.car_id=o.car_id")
            ->join($prefix . "car_owner as co ON co.owner_id=c.owner_id")
            ->join($prefix . "car_dealer as cd ON cd.dealer_id=o.success_dealer_id")
            ->join($prefix . "dealer_user as du ON du.dealer_id=cd.dealer_id")
            ->join($prefix . "car_dealer_check as dc ON dc.order_id=o.order_id")
            ->where($map)->limit($size)->page($page)->order($order)->select();
        //echo M()->getLastSql();die;
        $dealer = new Dealer();
        if (!empty($list)) {
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
        //echo M()->getLastSql();
        //数据处理
        if (!empty($list)) {
            $cm = new CarModel();
            $addr = new \AuctionCore\Other\Address();
            foreach ($list as $key => $val) {
                $list[$key] = $val;
                if ($list[$key]['is_sign_tracing'] == 0) {
                    $list[$key]['sign_reserve_time'] = transform_date($val['sign_reserve_time'], 1);
                }
                $list[$key]['location_area_name'] = $addr->get_name_by_code($val['location_area']);
                $list[$key]['car_plate_number'] = C('PLATE_PREFIX')[$val['plate_prefix']] . $val['plate_suffix'];
                $brand = $cm->get_model_by_id($val['brand_id'], 'brand');
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $model = $cm->get_model_by_id($val['model_id']);
                if ($val['sign_service_method'] == 2) {
                    $province = $addr->get_name_by_code($val['sign_reserve_province']);
                    $city = $addr->get_name_by_code($val['sign_reserve_city']);
                    $area = $addr->get_name_by_code($val['sign_reserve_area']);
                    $list[$key]['address'] = $province . " " . $city . " " . $area . " " . $val['sign_reserve_address'];
                } elseif ($val['sign_service_method'] == 3) {
                    $list[$key]['address'] = C('STORE.' . $val['sign_reserve_store']);
                } else {
                    $list[$key]['address'] = "未知";
                }
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                if ($params['auth']) {
                    $deliver = Employee::user_info_id($val['deliver_id']);
                    $list[$key]['deliver_name'] = $deliver['real_name'];
                }
                //获取操作人
                if ($list[$key]['emp_id'] > 0) {
                    $list[$key]['operator'] = M("employee")->where(['emp_id' => $list[$key]['emp_id']])->getField("real_name");
                }
                //截止验车时间显示
                if ($val['check_status'] == 0 || $val['check_status'] == 1) {
                    $color = "#303030";
                    $timeDiff = strtotime($val['check_limit_time']) - time();
                    if ($timeDiff < 0) {
                        $color = "#da0303";
                    } elseif ($timeDiff > 0 && $timeDiff < 21600) {
                        $color = "#f99d0c";
                    }
                    $list[$key]['color'] = $color;
                }
                //是否显示违约
                $list[$key]['is_again_void'] = true;
                // $changeInfo = changeOrder::detailMap(['order_id'=>$val['order_id'],'order_status'=>$val['status']]);
                // if ($changeInfo) {
                //     $list[$key]['is_again_void'] = false;
                // }
                if ($val['is_lock'] == 1) {
                    $list[$key]['is_again_void'] = false;
                }
                //是否显示争议
                $list[$key]['is_show_dispute'] = true;
                if ($val['is_lock'] == 1) {
                    $list[$key]['is_show_dispute'] = false;
                }
                $disInfo = M('dispute')->where(['order_id' => $val['order_id'], 'order_status' => $val['status'], 'od_type' => 1])->find();
                if ($disInfo) {
                    $list[$key]['is_show_dispute'] = false;
                }
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 保存车商验车处理结果
     *
     * 更改争议处理状态，"检验通过"=>"验车成功", "车况不符合|车商违约"=>"验车失败"
     * 验车通过：如果是金牌车商，可以选择过户类型；当选择车商自过类型时需要记录提车人
     * 车况不符：
     * 记录备注信息
     *
     * @param  array $data 需更新数据
     * @return  boolean
     */
    public function save_car_dealer_check($data = array())
    {
        if (!isset($data['check_id']) || !is_numeric($data['check_id'])) {
            AuctionException::make_throw('1700001');
        }
        if (!isset($data['status']) || !is_numeric($data['status'])) {
            AuctionException::make_throw('1700002');
        }
        $now = date('Y-m-d H:i:s');
        //事务开始
        M()->startTrans();
        // 记录车商验车处理结果
        $params = array(
            'status' => $data['status'],
            'emp_id' => $data['emp_id'],
            'remark' => $data['remark'],
            'updatetime' => $now
        );
        $o = new AuOrder($data['order_id']);
        $order_info = $o->detail();
        $car = new Car($order_info['car_id']);
        $carInfo = $car->detail();
        $checkInfo = self::detail_by_check_id($data['check_id']);
        //$payStatus = PurchaseOrder::getPayStatusBySid($carInfo['sid']);
        $payStatus = $carInfo['pay_status'];
        $checkLogData = [
            'check_id' => $data['check_id'],
            'emp_id' => $data['emp_id'],
            'emp_name' => $data['emp_name'],
            'remark' => $data['remark'],
            'create_at' => $now,
        ];
        if ($data['status'] == self::$dataStatus['success']) { // 验车通过
            if (isset($checkInfo['status']) && $checkInfo['status'] != self::$dataStatus['following']) {
                M()->rollback();
                AuctionException::make_throw('21000001');
            }
            $params['emp_id'] = $data['emp_id'];
            $params['check_time'] = $data['check_time'];
            $params['man_id'] = $data['man_id'];
            $checkLogData['action'] = "验车通过";
            $paramsOrder = array(
                'check_car_status' => 3, //  (1:待验车, 2:验车中, 3:验车成功, 4:车况不符, 5:违约)
            );
            //直接流转到过户
            if ($payStatus > 1) {
                $paramsOrder['status'] = 9; // 拍单状态(-1 删除,1审核中,2审核驳回,3投标中,301等待竞标,4竟标中,5待确认,7待签约,8待付首款，801已付首款,9待过户,10过户中,11待付尾款,12拍单失败,13拍单成功
                $transferLog = [];
                $transferLog['order_id'] = $data['order_id'];
                $transferLog['sid'] = $carInfo['sid'];
                \AuctionBack\Logic\TransferLogic::addTransferLog($transferLog);
                //发送消息
                $uid = $o->get_uid_by_order();
                $car_name = $car->get_car_name();
                $content = "【 " . $car_name . " 】拍单编号：" . $order_info['order_no'] . "，付款成功。交付顾问正在过户中，敬请关注。";
                Inbox::sendMessage($uid, '车辆待过户', $content);
            }
            //更新auctioned man_id
            $res = D("DealerAuctioned", "model")->where(['order_id' => $data['order_id'], 'dealer_id' => $order_info['success_dealer_id']])->save(['man_id' => $data['man_id']]);
            //更新过户信息
            $resOrder = $o->update($paramsOrder); // 待过户
            if ($resOrder === false) {
                M()->rollback();
                Log::record('车商验车时发生回滚事件！check_id:' . $data['check_id'] . ',emp_id:' . $data['emp_id']);
                return false;
            }
            $actionCar = '车商验车，验车成功';
        } elseif ($data['status'] == self::$dataStatus['following'] && $data['from'] == self::$dataStatus['pending']) { // 通知验车
            if (isset($checkInfo['status']) && $checkInfo['status'] != self::$dataStatus['pending']) {
                M()->rollback();
                AuctionException::make_throw('21000001');
            }
            $checkLogData['action'] = "通知验车";
            $actionCar = '车商验车，通知验车';
        } elseif ($data['status'] == self::$dataStatus['following'] && $data['from'] == self::$dataStatus['following']) { //继续跟踪
            if (isset($checkInfo['status']) && $checkInfo['status'] != self::$dataStatus['following']) {
                M()->rollback();
                AuctionException::make_throw('21000001');
            }
            $params['next_trace_time'] = $data['next_trace_time'];
            $checkLogData['action'] = "继续跟踪";
            $actionCar = '车商验车，继续跟踪';
        }

        $car_dealer_check = M('car_dealer_check');
        $res_car_dealer_check = $car_dealer_check->where(array('check_id' => $data['check_id']))->save($params);
        if ($res_car_dealer_check) {
            //事务提交
            M()->commit();
            //验车跟踪日志
            $this->addCarDealerCheckLog($checkLogData);
            //记录操作日志
            $log = array('check_id' => $this->check_id, 'emp_id' => $data['emp_id'], 'action' => '处理车商验车');
            \AuctionCore\System\SystemLog::add($log);
            //记录车辆跟踪日志
            $car_action_data = array(
                'action_name' => $actionCar,
                'emp_id' => $data['emp_id']
            );
            \AuctionCore\CarResource\Car::add_car_trace_log($data['car_id'], $car_action_data);
            /*记录拍品跟踪日志*/
            $order_action_data = array(
                'action_name' => $actionCar,
                'emp_id' => $data['emp_id']
            );
            AuOrder::add_order_trace_log($data['order_id'], $order_action_data);
            return true;
        } else {
            M()->rollback();
            \Think\Log::record('车商验车时发生回滚事件！check_id:' . $data['check_id'] . ',emp_id:' . $data['emp_id']);
            return false;
        }
    }

    /**
     *  过户违约 发生争议
     **/
    public static function transfer_dealer_issue($data = array())
    {
        //事务开始
        M()->startTrans();
        $o = new AuOrder($data['order_id']);
        /*修改拍单表，标记产生车商争议*/
        $paramsOrder = array(
            'is_dealer_issue' => 1,
        );
        $resOrder = M('order')->where(array('order_id' => $data['order_id']))->save($paramsOrder);

        /*添加车商争议初始记录*/
        $paramsIssue = array(
            'order_id' => $data['order_id'],
            'dealer_id' => $data['dealer_id'],
            'issue_type' => $data['issue_type'], // 争议类型(1:车况不符, 2:超时验车,3:过户争议,4:付款超时)'
            'status' => 0, // 争议处理结果(0:待处理, 1:协商成功 2:协商失败)
        );
        $paramsIssue['deduct_type'] = 2; // 扣款类型(1:冻结保证金中扣除, 2:车款中扣除)
        $resIssue = \AuctionCore\Issue\DealerIssue::add_dealer_issue($paramsIssue);
        if ($resOrder === false || $resIssue === false) {
            M()->rollback();
            \Think\Log::record('过户违约时发生回滚事件！order_id:' . $data['order_id'] . ',emp_id:' . $data['emp_id']);
            return false;
        } else {
            return true;
        }
    }

    /**
     * 车商付款超时争议
     * @param array $data
     * @param $proceeds_log
     * @return bool
     */
    public static function dealer_pay_issue($data, $proceeds_log)
    {
        //事务开始
        M()->startTrans();
        $o = new AuOrder($data['order_id']);
        /*添加车商争议初始记录*/
        $paramsIssue = array(
            'order_id' => $data['order_id'],
            'dealer_id' => $data['dealer_id'],
            'issue_type' => 4, // 争议类型4 结算争议
            'status' => 0, // 争议处理结果(0:待处理, 1:协商成功 2:协商失败)
        );
        $orderInfo = $o->detail();
        if ($orderInfo['dealer_pay_status'] == 0) {
            $paramsIssue['issue_type'] = 4; //结算争议
        } elseif ($orderInfo['dealer_pay_status'] == 1) {
            $paramsIssue['issue_type'] = 6; //待付款争议
        } elseif ($orderInfo['dealer_pay_status'] == 2) {
            $paramsIssue['issue_type'] = 7; //结算争议
        } else {
            M()->rollback();
            Log::record("付款状态不正确", "ERR", true);
            return false;
        }
        $paramsIssue['deduct_type'] = 1; // 扣款类型(1:冻结保证金中扣除)
        $DealerIssue = new DealerIssue($data['order_id']);
        $resIssue = $DealerIssue->add_dealer_issue($paramsIssue);
        if ($resIssue == false) {
            M()->rollback();
            Log::record('付款超时,添加争议记录时发生回滚事件！order_id:' . $data['order_id'] . ',emp_id:' . $data['emp_id'], 'ERR');
            return false;
        }

        $proceeds = $o->obtain_trace_log('proceeds_log');
        if ($proceeds) {
            $log_result = $o->update_trace_log('proceeds_log', $proceeds_log);
        } else {
            $proceeds_log['createtime'] = date('Y-m-d H:i:s');
            $log_result = $o->set_trace_log('proceeds_log', $proceeds_log);
        }
        if ($log_result == false) {
            M()->rollback();
            \Think\Log::record('付款超时,付款记录更新时发生回滚事件！order_id:' . $data['order_id'] . ',emp_id:' . $data['emp_id']);
            return false;
        }
        /*修改拍单表，标记产生车商争议*/
        $paramsOrder = array(
            'is_dealer_issue' => 1,
            'dealer_pay_status' => 4//车商付款状态（0未确认 ,1未付款，2，待核实 3，已付款 4 , 锁定 5,收款失败)
        );
        $resOrder = $o->update($paramsOrder);
        if ($resOrder == false) {
            M()->rollback();
            Log::record('付款超时,更新拍单时发生回滚事件！order_id:' . $data['order_id'] . ',emp_id:' . $data['emp_id']);
            return false;
        }
        return true;
    }


    public function get_car_check($order_id)
    {
        $res = M('car_dealer_check')->where("order_id='" . $order_id . "'")->find();
        return $res;
    }

    /**
     * 根据验车ID更新验车信息
     * @param $checkId
     * @param $data
     * @return bool
     */
    public function updateByCheckId($checkId, $data)
    {
        if ($checkId < 1 || empty($data)) {
            return false;
        }
        return M("car_dealer_check")->where(['check_id' => $checkId])->save($data);
    }

    /**
     * 新增验车跟踪记录
     * @param $data
     * @return bool|mixed
     */
    public function addCarDealerCheckLog($data)
    {
        if (empty($data)) {
            return false;
        }
        return M("car_dealer_check_log")->add($data);
    }

    /**
     * 根据验车ID获取验车跟踪记录
     * @param $checkId
     * @return array|mixed
     */
    public function getCarDealerCheckLogListByCheckId($checkId)
    {
        if ($checkId < 1) {
            return [];
        }
        $list = M("car_dealer_check_log")->where(['check_id' => $checkId])->select();
        return $list;
    }
}
